package com.xyzwps.server.paimon.app

import com.xyzwps.server.paimon.app.dto.MeUpdateInput
import com.xyzwps.server.paimon.app.filter.AppFilter
import com.xyzwps.server.paimon.app.dto.Me
import com.xyzwps.server.paimon.app.filter.AppFilterToken.*
import com.xyzwps.server.paimon.error.Errors
import com.xyzwps.server.paimon.model.service.UserDomainService
import org.springframework.stereotype.Service

@Service
class MeAppService(
    private val userDomainService: UserDomainService
) {

    @AppFilter([AUTH])
    fun me(user: ContextUser): Me {
        return Me(user.user)
    }

    @AppFilter([AUTH])
    fun meUpdate(user: ContextUser, input: MeUpdateInput): Me {
        val meUser = userDomainService.getById(user.id) ?: throw Errors.IMPOSSIBLE.toEx()
        val updatedUser = meUser.copy(nickname = input.nickname)
        userDomainService.save(updatedUser)
        return Me(updatedUser)
    }
}
